/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package botlab.control;

import botlab.Game;
import botlab.engine.physics.RayCast;
import com.jme3.math.Quaternion;
import com.jme3.math.Vector3f;
import com.jme3.scene.Node;
import com.jme3.scene.Spatial;
import com.jme3.scene.control.Control;

/**
 *
 * @author Quentin
 */
public class PatrolControl extends BaseControl
{
    private Vector3f direction;
    private Vector3f position;
    private RayCast rc;
    private Node rayCastables;
    
    public PatrolControl(Vector3f direction,Vector3f position,Spatial spatial)
    {
        this.direction = direction;
        this.position = position;
        rayCastables = (Node) Game.getApp().getCurrentLevel().getRayCastableNode();
        
        Quaternion quat = new Quaternion();
        quat.lookAt(direction, new Vector3f(0, 1, 0));
        spatial.setLocalRotation(quat);
        spatial.setLocalTranslation(position);
        rc = new RayCast(rayCastables,new Node[]{(Node)spatial});
    }

    @Override
    protected void controlUpdate(float tpf)
    {
        //go forward
        spatial.setLocalTranslation(spatial.getLocalTranslation().add(
                    spatial.getLocalRotation().mult(Vector3f.UNIT_Z.mult(tpf))));
        
        //distance before collision
        rc.rayCastFromPoint(spatial.getLocalTranslation(), direction);
        float dst = rc.getDistance();
        
        
        // turn over
        if(dst<2)
        {
            direction = direction.mult(-1);
            Quaternion quat = new Quaternion();
            quat.lookAt(direction, new Vector3f(0, 1, 0));
            spatial.setLocalRotation(quat);
        }
        
    }

    @Override
    public Control cloneForSpatial(Spatial spatial)
    {
        throw new UnsupportedOperationException("Not supported yet. PatrolControl");
        /*final PatrolControl control = new PatrolControl(direction,position,spatial);
        control.setSpatial(spatial);
        return control;*/
    }


    
}
